
import org.authsum.stitches.*
import org.springframework.web.servlet.ModelAndView
import org.authsum.stitches.external.*
import org.authsum.stitches.services.*

class TagsController {
	
	def stitchesService
	
	def deleteTag = {
			
		def tagRequest = new TagRequest()
		bindData(tagRequest, params)
		def response =stitchesService.deleteTag(tagRequest)
		
		assert response.responseCode == '200'
		flash.message = 'tag deleted'
		
		chain(action:index)
	}
	
	
	//this is a request to edit a tag, get the tag, and go to the edit screen
	def editTag = {
			
		def tagRequest = new TagRequest()
		bindData(tagRequest, params)
		def tagResponse =stitchesService.findTagById(tagRequest)
		def tagHolder =tagResponse.tagHolders.tagHolder.get(0)
		assert tagResponse.responseCode == '200'

			return new ModelAndView("/tags/tagForm",  [tagHolder:tagHolder])
	}
	
	
	def addTagToContentForm= {}
	
	def addTagToContent = {
			
		def tagRequest = new TagRequest()
		bindData(tagRequest, params)
		def response =stitchesService.addTagToContent(tagRequest)
		
		assert response.responseCode == '200'
		chain(action:index,model:[childFolders:response]	)
	}
	
	
	
	
	def index = { 
			
		def tagRequest = new TagRequest()
		tagRequest.max = 100
		def tagResponse =stitchesService.searchTags(tagRequest)
		
		println "tag responsecode" 
		//  for (tag in tagResponse.tagHolders) println "hi " + tag.value
		
		
		[ tagResponse:tagResponse] 		
		
		
		
	}
	
	def createTag = { 
		

			
			def tagHolder = new TagHolder()
			

			return new ModelAndView("/tags/tagForm",  [tagHolder:tagHolder])
			
	}
	def saveTag = { 
			
		def tagRequest = new TagRequest()
		
		def tagHolder = new TagHolder()
		
		if (!tagRequest.tagHolders) {
			def tagHolders = new ArrayOfTagHolder()
			tagRequest.tagHolders = tagHolders
			tagRequest.tagHolders.tagHolder.add(tagHolder)
		}		 
		bindData(tagHolder, params)
						//TEMP, need to set the id manually
				if (params['id']) {
				tagHolder.id = params['id'].toLong()
				}
				
				
		def response =stitchesService.saveTag(tagRequest)
		
		assert response.responseCode == '200'
		flash.message = 'tag saved'
		
		chain(action:index)
		
		
		
	}
	
	
}
